package com.example.admin.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.example.admin.Dto.Beddto;
import com.example.admin.Dto.Floordto;
import com.example.admin.Dto.RoomDevicedto;
import com.example.admin.Dto.Roomdto;
import com.example.admin.bean.DeviceData;
import com.example.admin.mapper.Floormapper;
import com.example.admin.service.Floorservice;
import com.example.admin.service.Lotservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImpFloorservice implements Floorservice {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    Floormapper floormapper;
    @Autowired
    Lotservice lotservice;
    @Override
    public Floordto[] getAllFloorsWithDevice() {
        Floordto[] floordtos =  floormapper.getAllFloorsWithDevice();
        return floordtos;
    }

    @Override
    public Roomdto[] getRoomsWithDeviceByFloorId(Long floorId) {
       Roomdto[] roomdto = floormapper.getRoomsWithDeviceByFloorId(floorId);
        String data = redisTemplate.opsForValue().get("redis:bed:data").toString();
        DeviceData deviceData = JSONObject.parseObject(data, DeviceData.class);
        for (Roomdto roomdto1 : roomdto) {
            List<Beddto> beds = roomdto1.getBed();
            for (Beddto bed : beds) {
                List<RoomDevicedto> roomDevicedto = bed.getRoomDevicedto();
                for (RoomDevicedto devicedto : roomDevicedto) {
                    devicedto.setDeviceData(deviceData);
                }
            }
        }
       return roomdto;
    }
}
